package com.flyco.modules.oms.handle.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flyco.common.util.DateUtils;
import com.flyco.common.util.HttpUtil;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.handle.IOmsOrderHandle;
import com.flyco.modules.util.ApiUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Component
public class OmsOrderHandleImpl implements IOmsOrderHandle {

    @Override
    public String getOmsOrderStatus(String orderId) {
        String timestamp = DateUtils.now();
        StringBuilder url = new StringBuilder();
        url.append(ApiUtil.getOmsUrl());
        url.append("server/api/Rest/WingApi?");
        String password = "028e5b85075e46a0873895a2e89b8e14";
        String APP_KEY="flyco_sign";
        Map<String, String> maps = new HashMap<>();
        Map<String, Object> mapUrls = new HashMap<>();
        maps.put("sourceCode", orderId);
        mapUrls.put("APP_KEY",APP_KEY);
        mapUrls.put("METHOD", "Burgeon.Wing.OnlineOrders.OrderStatus.Get");
        mapUrls.put("TIMESTAMP", timestamp);
        mapUrls.put("V", "1");
//        mapUrls.put("BODY", JSONObject.toJSON(maps));
        mapUrls.put("sourceCode", orderId);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(password);
        String SIGN = "";
        List<Object> keys = Arrays.asList(mapUrls.keySet().toArray());
        Collections.sort(keys, new Comparator<Object>() {
            public int compare(Object s1, Object s2) {
                /**
                 * 升序排的话就是第一个参数.compareTo(第二个参数);
                 * 降序排的话就是第二个参数.compareTo(第一个参数);
                 */
                return s1.toString().compareToIgnoreCase(s2.toString());
            }
        });
        for (Object key : keys) {
            Object value = mapUrls.get(key.toString());
            stringBuffer.append(String.format("%s%s", key, value));
            //System.out.println(key + "------->" + value);
            if (key.toString().equals("BODY") == false) {
                try {
                    url.append(String.format("%s=%s&", key, java.net.URLEncoder.encode(value.toString(), "UTF-8")));
                    //url += String.format("%s=%s&", key, java.net.URLEncoder.encode(value.toString(), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            SIGN = DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes("utf-8")).toUpperCase();
            url.append("SIGN=" + SIGN);
            //url += "SIGN=" + SIGN;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //System.out.println("签名--> " + SIGN);
        //System.out.println("请求地址--> " + url);

        String result = HttpUtil.getRequest(url.toString(), maps, null);
        System.out.println("返回--> " + result);

        if (! oConvertUtils.isEmpty(result)) {
            JSONObject resultObj = JSON.parseObject(result);
            if ("0".equals( resultObj.get("Code") )) {
                JSONArray jsonArray = resultObj.getJSONArray("Rows");
                if (jsonArray.size() > 0) {
                    return jsonArray.getJSONObject(0).getString("OrderStatus");
                }
            }
        }
        return null;
    }
}
